/**
 * 
 */
package fr.ssd.sdl.store.mux;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import fr.ssd.sdl.store.Store;
import fr.ssd.sdl.store.StoreException;

/**
 * This class provides a sequential implementation of the store multiplexer. It
 * means each backend is invoked sequentially.
 * 
 * @author Sylvain Sicard
 * 
 */
public class SequentialMultiplexer implements Store, Multiplexer {

	/**
	 * The list of backends handled by this multiplexer.
	 */
	List<Store> backends = new LinkedList<Store>();

	/**
	 * Broadcast the {@code init(hints)} method to each backend
	 */
	@Override
	public void init(Map<String, ?> hints) throws StoreException {
		for (Store s : backends) {
			s.init(hints);
		}
	}

	/**
	 * Broadcast the {@code store(sqlStatement} method to each backend
	 */
	@Override
	public void store(String sqlStatement) throws StoreException {
		for (Store s : backends) {
			s.store(sqlStatement);
		}
	}

	/**
	 * Broadcast the {@code terminate()} method to each backend
	 */
	@Override
	public void terminate() throws StoreException {
		for (Store s : backends) {
			s.terminate();
		}
	}

	/**
	 * 
	 */
	@Override
	public void addStore(Store store) {
		backends.add(store);
	}

	/**
	 * 
	 */
	@Override
	public void removeStore(Store store) {
		backends.remove(store);
	}

}
